#include "scanner.h"
#include <Windows.h>
#include <iostream>

Scanner::Scanner()
{
    _screenx = GetSystemMetrics(SM_CXSCREEN);
    _screeny = GetSystemMetrics(SM_CYSCREEN);
    ClearCurrentMap();
    ClearOldMap();
}

void Scanner::ClearCurrentMap()
{
    for(int i=0; i<_minimapheight; i++)
    {
        for(int j=0; j<_minimapwidth; j++)
        {
            _currentMap[j][i]=0;
        }
    }
}

void Scanner::ClearOldMap()
{
    for(int i=0; i<_minimapheight; i++)
    {
        for(int j=0; j<_minimapwidth; j++)
        {
            _oldMap[j][i]=0;
        }
    }
}


/// <summary>
//  Starts the Scanner
/// </summary>
void Scanner::Start()
{

    // check only every 100ms
    Sleep(100);
    GetColorValues();
    CheckMap();

}

/// <summary>
//  Stops the Scanner
/// </summary>
bool Scanner::Stop()
{
    return true;
}

/// <summary>
// scans the minimap for red dots and sets the found location in _map
/// </summary>
void Scanner::GetColorValues()
{
    // copy to old map
    for(int i=0; i<_minimapheight; i++)
    {
        for(int j=0; j<_minimapwidth; j++)
        {
            _oldMap[j][i]=_currentMap[j][i];
        }
    }

    // clear the current values
    ClearCurrentMap();

    int colorindex = (_screeny - _minimapstarty)*_screenx+_minimapstartx;

    // information of the DIB, stores color information

    BITMAPINFO info = {0};
    info.bmiHeader.biSize     = sizeof(BITMAPINFOHEADER);
    info.bmiHeader.biWidth    = _screenx;
    info.bmiHeader.biHeight   = _screeny;
    info.bmiHeader.biPlanes   = 1;
    info.bmiHeader.biBitCount = 32; // 32:RGBQUAD; 24:RGBTRIPLE


    RGBQUAD* currentColors;

    // get the desktop handle
    HDC hdcDesktop = GetDC(0);
    // get current bitmap
    HBITMAP bitmap = CreateDIBSection(hdcDesktop, &info, DIB_RGB_COLORS, (void**)&currentColors, 0, 0);

    if(!bitmap || !currentColors) return;

    // create a compatible handle for the memory
    HDC hdcMemory  = CreateCompatibleDC(hdcDesktop);
    // selects
    SelectObject(hdcMemory, bitmap);
    // copy from desktop to memory
    BitBlt(hdcMemory, 0, 0, _screenx, _screeny, hdcDesktop, 0, 0, SRCCOPY);

    ReleaseDC(0, hdcDesktop);
    int x=0, y=0;
    for(int i = 0; i < _minimapheight; i++)
    {
        for(int j = 0; j < _minimapwidth; j++, colorindex++)
        {
            RGBQUAD *p = &currentColors[colorindex];
            y = colorindex / _screenx;
            x = colorindex % _screenx;
            y = _screeny - y;

            if(p->rgbRed==180 && p->rgbGreen==20 && p->rgbBlue==30)
            {
                _currentMap[j][i] = 1;
            }
        }

        colorindex += _screenx-_minimapwidth;
    }
    //cout << "checked" << endl;
    DeleteObject(bitmap);
//    ReleaseDC(0, hdcMemory);
}

/// <summary>
//  checks the map and beeps if something interesting happend
/// </summary>
void Scanner::CheckMap()
{



    ClearOldMap();
}
